<template>
  <div class="content">
    <!-- 导航栏 -->
    <div class="navbar">
      <div
        class="nav-item"
        :class="{ active: activeTab === 'album' }"
        @click="switchTab('album')"
      >
        专辑榜
      </div>
      <div
        class="nav-item"
        :class="{ active: activeTab === 'wall' }"
        @click="switchTab('wall')"
      >
        壁纸榜
      </div>
    </div>

    <!-- 专辑榜 -->
    <div class="album-list" v-if="activeTab === 'album'">
      <div
        class="album-item"
        v-for="item in albumList"
        :key="item.id"
        @click="goPreviewAlbum(item.id)"
      >
        <div class="rank">{{ item.rank }}</div>
        <img :src="item.imgUrl" alt="专辑封面" />
        <div class="nickname">著：{{ item.nickname }}</div>
      </div>
    </div>

    <!-- 壁纸榜 -->
    <div class="wall-list" v-if="activeTab === 'wall'">
      <div
        class="wall-item"
        v-for="item in wallList"
        :key="item.id"
        @click="goPreview2(item.id)"
      >
        <div class="rank">{{ item.rank }}</div>
        <img :src="item.smallPicurl" alt="壁纸" />
        <!-- 其他可能的数据显示 -->
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { listWallRank } from "@/api/wallpaper/wallRank";
import { listAlbumRank } from "@/api/wallpaper/albumRank";
import { useRouter } from "vue-router";
const router = useRouter();

// 数据状态
const activeTab = ref("album");

// 方法
const switchTab = (tab) => {
  activeTab.value = tab;
  if (tab === 'album') {
    getAlbumRank();
  } else {
    getWallRank();
  }
  console.log('Fetched data:', tab === 'album' ? albumList.value : wallList.value); // 添加日志
};

const albumList = ref([]); // 新增专辑榜列表
const getAlbumRank = async () => {
  try {
    const res = await listAlbumRank();
    // console.log(res);
    if (res.code==200) {
      albumList.value = res.rows.sort((a, b) => a.rank - b.rank);
      // console.log('album',albumList.value);
    }
  } catch (error) {
    console.error("API请求失败:", error);
  }
};

const wallList = ref([]);
const getWallRank = async () => {
  try {
    const res = await listWallRank();
    if (res.code==200) {
      wallList.value = res.rows.sort((a, b) => a.rank - b.rank);
      // console.log(wallList.value);
    }
  } catch (error) {
    console.error("API请求失败:", error);
  }
};

onMounted(() => {
  getAlbumRank(); // 获取专辑榜数据
});

// 壁纸跳转到预览页面
const goPreviewAlbum = (id) => {
  router.push(`/preview/album/${id}`);
};

// 壁纸跳转到预览页面
const goPreview2 = (id) => {
  localStorage.setItem("storgClassList", JSON.stringify(wallList.value));
  router.push(`/preview/wallpaper/${id}`);
};
</script>

<style scoped lang="scss">
// 基本变量定义
$primary-color: #409eff;
$font-size-base: 16px;
$line-height-base: 1.5;
$border-radius-base: 4px;

// 全局样式
body {
  font-family: Arial, sans-serif;
  margin: 0;
  padding: 0;
  background: #f5f5f5;
}

// 内容容器
.content {
  padding: 20px;
  height: 100vh; // 让内容区域占据整个屏幕高度
  overflow-y: auto; // 允许垂直滚动

  .navbar {
    display: flex;
    justify-content: space-around;
    margin-bottom: 20px;

    .nav-item {
      font-size: 18px;
      color: #999;
      padding: 10px;
      cursor: pointer;
      transition: color 0.3s, border-bottom 0.3s;

      &.active {
        color: $primary-color;
        border-bottom: 2px solid $primary-color;
      }

      &:hover {
        color: darken($primary-color, 10%);
      }
    }
  }

  .wall-list,
  .album-list {
    padding: 30px;
    display: grid;
    gap: 15px;
    max-width: 100%; // 最大宽度为100%
    width: 100%; // 宽度为100%

    .rank {
      position: absolute;
      top: 10px;
      left: 10px;
      background-color: rgba(
        0,
        0,
        0,
        0.5
      ); // 背景颜色稍微透明一些，以确保数字清晰可见
      color: white; // 文字颜色为白色
      padding: 5px 10px; // 给数字周围留一点空间
      border-radius: 5px; // 圆角边框
      font-size: 18px; // 字体大小
      z-index: 1; // 确保排名数字位于图片之上
    }

    img {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      object-fit: cover; // 图片覆盖整个区域
      border-radius: $border-radius-base;
      box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
      transition: transform 0.3s, box-shadow 0.3s;

      &:hover {
        transform: scale(1.05);
        box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
      }
    }
  }

  .wall-list {
    grid-template-columns: repeat(5, 1fr); // 每行五列

    .wall-item {
      width: 100%;
      padding-bottom: 200%; // 保持图片宽高比为1:2
      position: relative;
    }
  }

  .album-list {
    grid-template-columns: repeat(4, 1fr); // 每行四列，根据需要调整

    .album-item {
      width: 100%;
      padding-bottom: 150%; // 保持图片宽高比为1:1.5，根据需要调整
      position: relative;

      .nickname {
        position: absolute;
        bottom: 10px;
        left: 10px;
        background-color: rgba(0, 0, 0, 0.5);
        color: white;
        padding: 5px 10px;
        border-radius: 5px;
        font-size: 16px;
        z-index: 1;
      }
    }
  }
}
</style>
